package com.lcvc.xgy.springboot.webcontroller.backstage;


import com.lcvc.xgy.springboot.model.Admin;
import com.lcvc.xgy.springboot.service.Adminbean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller//声明spring控制器
//声明地址
@RequestMapping("/backstage")
public class LoginLogoutController {

    @Autowired
    private Adminbean adminbean;


    @PostMapping("/login")
    @ResponseBody
    public Map<String, Object> login(String username, String password, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        if (adminbean.login(username, password)) {
//登录成功后保存登录信息，并将账户信息发送至前端
            Admin admin = adminbean.getAdmin(username);
            session.setAttribute("admin", admin);
            map.put("data", admin);
            map.put("code", 200);
            map.put("message", "登录成功");

        } else {
            map.put("code", -1);
            map.put("message", "登录失败");
        }
        map.put("time", new Date());
        return map;
    }

    @GetMapping("/loginout")
    @ResponseBody
    public Map<String, Object> loginout(HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        // 移除保存的信息
        session.removeAttribute("admin");
        map.put("code", 200);
        map.put("time", new Date());
        return map;

    }


}